<? include "checksession.php"; ?>
<?php
header("Content-type:text/x-json; charset=UTF-8");        
header("Cache-Control: no-store, no-cache, must-revalidate");       
header("Cache-Control: post-check=0, pre-check=0", false);       

include "../connect.php";   

$page = $_POST['page']; 
$rp = $_POST['rp']; 
$sortname = $_POST['sortname']; 
$sortorder = $_POST['sortorder']; 
 

if (!$sortname) $sortname = 'pd_code'; 
if (!$sortorder) $sortorder = 'desc'; 
if (!$page) $page = 1; //  
if (!$rp) $rp = 10; 
 

$start = (($page-1) * $rp);
$limit = "LIMIT $start, $rp";
$sort = "ORDER BY $sortname $sortorder";
 

$q = "SELECT * FROM `product_detail` WHERE `pd_status` = 'IsUse' ";
$qr = mysql_query($q);
$total = mysql_num_rows($qr);
$query = $_POST['query']; 
$qtype = $_POST['qtype']; 
 
$where = "";
if ($query) $where = "and $qtype LIKE '%$query%' ";

$q = "SELECT * FROM `product_detail` inner join product on product.prd_id = product_detail.prd_id inner join prd_type on prd_type.type_id = product.type_id where pd_status = 'IsUse' $where $sort  $limit";

$qr = mysql_query($q);
$numrow=mysql_num_rows($qr);
if($numrow>0){
$data['page'] = intval($page); 
$data['total'] = intval($total); 
while ($row = mysql_fetch_array($qr)) {
		$rows[] = array(
				"id" => $row['prd_id'],
				"cell" => array(
				    $row['prd_name']
	               ,$row['pd_codeAdinop']
					,$row['pd_code']
					,$row['type_name']
					,$row['emp_id']
					,$row['pd_outDate']
				)
			);	
}
}else { 
     		$rows[] = array(
				"id" => 'null',
				"cell" => array(
					'null'
					,'null'
				    ,'null'
					,'null'
	               ,'null'
				   ,'null'
				)
			);		
 
}
 
$data['rows'] = $rows;
echo json_encode($data);

exit;
 
?>